package com.school.controller.common;


import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//@Component
@RestController
public class CommonController {

    @GetMapping("/common/kaptcha")
    public void defaultKaptcha(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // Set response headers to prevent caching
        resp.setHeader("Cache-Control", "no-store, no-cache");
        resp.setHeader("Pragma", "no-cache");
        resp.setDateHeader("Expires", 0);

        // Set content type to image/gif
        resp.setContentType("image/gif");

        // Create a new CAPTCHA with specified dimensions and length
        SpecCaptcha captcha = new SpecCaptcha(150, 48, 4);
        captcha.setCharType(Captcha.TYPE_DEFAULT);
        captcha.setFont(SpecCaptcha.FONT_1);

        // Store the CAPTCHA text in session (converted to lowercase)
        req.getSession().setAttribute("verifyCode", captcha.text().toLowerCase());

        // Write the CAPTCHA image to the response output stream
        captcha.out(resp.getOutputStream());
    }
}

